package it.marco.test.controller;

import it.marco.test.dm.Persona;
import it.marco.test.service.PersonaService;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value="/persona")
public class PersonaController {
	
	@Autowired
	private PersonaService<Persona, Long> serv;
	
//	@Autowired
//	private Validator validator;
//	
//	public void setValidator(Validator validator) {  
//        this.validator = validator;  
//	}  
	
	@RequestMapping(method=RequestMethod.GET)
	public String getIndex(ModelMap model){
		List<Persona> lp = serv.findAll();
		
		model.addAttribute("lista",lp);
		model.addAttribute("test2", "prova2");
		Persona p = new Persona();
		p.setNome("Nome1");
		p.setCognome("wwwwpop");
		model.addAttribute("persona", p);
		return "persona";
//		return new ModelAndView("index","test", "Ciao a tutti");
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String salva(@Valid @ModelAttribute("persona") Persona p, BindingResult result){
		System.out.println(p.toString());
		//validator.validate(p, result);
//		Assert.notNull(p.getEmail(), "Identifier must be provided.");
		serv.saveOrUpdate(p);
		return "persona";
	}
}
